package com.chcit.mobile.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.chcit.custom.view.button.SwitchButton;
import com.chcit.custom.view.loader.QuickLoader;
import com.chcit.mobile.R;
import com.chcit.mobile.app.base.SupportToolbarActicity;
import com.chcit.mobile.app.contains.HttpMethodContains;
import com.chcit.mobile.app.utils.AppUtils;
import com.chcit.mobile.app.utils.BarcodeUtil;
import com.chcit.mobile.app.utils.SharedPreUtils;
import com.chcit.mobile.di.component.DaggerLoginComponent;
import com.chcit.mobile.di.module.LoginModule;
import com.chcit.mobile.helper.UpdateAppHttpUtil;
import com.chcit.mobile.mvp.contract.LoginContract;
import com.chcit.mobile.mvp.presenter.LoginPresenter;
import com.chcit.mobile.mvp.ui.fragment.LoginFragment;
import com.chcit.mobile.mvp.ui.fragment.ServerFragment;
import com.jess.arms.base.app.ConfigKeys;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.ArmsUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.vector.update_app.UpdateAppBean;
import com.vector.update_app.UpdateAppManager;
import com.vector.update_app.UpdateCallback;
import com.vector.update_app.listener.IUpdateDialogFragmentListener;

import javax.inject.Inject;

import butterknife.BindView;
import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportFragment;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class LoginActivity extends SupportToolbarActicity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.version_name)
    TextView versionName;
    @BindView(R.id.switch_button)
    SwitchButton switchButton;
    @BindView(R.id.linear_layout_testmodel)
    LinearLayoutCompat linearLayoutTestmodel;
    ISupportFragment[] fragments ;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLoginComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    private ImageView imageView ;
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        fragments =new  SupportFragment[]{LoginFragment.newInstance(),ServerFragment.newInstance()};
        ((ImageView)mCommonToolbar.getViewByTag(1)).setImageResource(R.mipmap.exit_32);
        mCommonToolbar.addRightText(3,"",null);
        mCommonToolbar.setTitle("");
        //mCommonToolbar.addRightIcon(4,R.drawable.ic_service,null);
        mCommonToolbar.getViewByTag(1).setOnClickListener(v->{
            BarcodeUtil.getInstance().destroyAidBroad();
            ArmsUtils.exitApp();

        });
        switchButton.setChecked(SharedPreUtils.getInstance().getBoolean(ConfigKeys.TEST_MODE.name(),false));
        switchButton.setOnCheckedChangeListener((v, b) -> {
            if (b) {
                SharedPreUtils.getInstance().putBoolean(ConfigKeys.TEST_MODE.name(), true);
            } else {
                SharedPreUtils.getInstance().putBoolean(ConfigKeys.TEST_MODE.name(), false);
            }

            AppUtils.relaunchApp();

        });
      initData();


    }
    private void initData(){
        if (findFragment(LoginFragment.class) == null) {

            //loadRootFragment(R.id.login_page, LoginFragment.newInstance());  // 加载根Fragment
            if(SharedPreUtils.getInstance().getBoolean("FIRST",true)){
                mCommonToolbar.addRightIcon(4, R.drawable.ic_login, 30, 30, new View.OnClickListener() {
                    int i = 2;
                    @Override
                    public void onClick(View v) {
                        if(i%2 == 0){
                            showHideFragment(fragments[0]);
                            imageView.setImageResource(R.drawable.ic_service);
                        }else{
                            showHideFragment(fragments[1]);
                            imageView.setImageResource(R.drawable.ic_login);
                        }
                        i++;
                    }
                });
                loadMultipleRootFragment(R.id.login_page,1, fragments);
            }else{
                mCommonToolbar.addRightIcon(4,R.drawable.ic_service,30,30,new View.OnClickListener() {
                    int i = 1;
                    @Override
                    public void onClick(View v) {
                        if(i%2 == 0){
                            showHideFragment(fragments[0]);
                            imageView.setImageResource(R.drawable.ic_service);
                        }else{
                            showHideFragment(fragments[1]);
                            imageView.setImageResource(R.drawable.ic_login);
                        }
                        i++;
                    }
                });
                loadMultipleRootFragment(R.id.login_page,0, fragments);
            }
            imageView = mCommonToolbar.getViewByTag(4);
        }
    }


    @Override
    public RxPermissions getRxPermissions() {
        return new RxPermissions(LoginActivity.this);
    }

    @Override
    public void toLogin() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarTextWithLong(message);
    }
    @Override
    public Context getActivity() {
        return this;
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
